package http;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 未测试 建议使用HTTP客户端（Hutool-http）
 *
 * 参考链接:https://blog.csdn.net/justry_deng/article/details/81042379
 */
public class HttpGetUtil {

    public static String doGet(HttpClient httpClient, String url, Map<String, String> paramHeader, HashMap<String, String> param, String charset) throws IOException {

        String result = null;

        String urlWithParam = setParam(url, param);
        HttpGet httpGet = new HttpGet(urlWithParam.toString());

        setHeader(httpGet, paramHeader);
//        setConfig(httpGet);

        HttpResponse response = httpClient.execute(httpGet);
        if (response != null) {
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                result = EntityUtils.toString(responseEntity, charset);
            }
        }

        return result;
    }

    private static void setHeader(HttpRequestBase httpGet, Map<String, String> paramHeader) {
        if (paramHeader != null) {
            paramHeader.forEach((key, value) -> {
                httpGet.addHeader(key, value);
            });
        }
    }

    private static String setParam(String url, HashMap<String, String> param) {
        StringBuilder stringBuilder = new StringBuilder(url);
        param.forEach((key, value) -> {
            stringBuilder.append("&");
            stringBuilder.append(key);
            stringBuilder.append("=");
            stringBuilder.append(value);
        });

        return stringBuilder.toString();
    }

    private static void setConfig(HttpRequestBase httpGet) {
        // 配置信息
        RequestConfig requestConfig = RequestConfig.custom()
                // 设置连接超时时间(单位毫秒)
                .setConnectTimeout(5000)
                // 设置请求超时时间(单位毫秒)
                .setConnectionRequestTimeout(5000)
                // socket读写超时时间(单位毫秒)
                .setSocketTimeout(5000)
                // 设置是否允许重定向(默认为true)
                .setRedirectsEnabled(true).build();

        // 将上面的配置信息 运用到这个Get请求里
        httpGet.setConfig(requestConfig);
    }

    public static void main(String[] args) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        String url = "https://5141176-sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=391&deploy=1";

        Map<String, String> paramHeader = new HashMap();
        paramHeader.put("Content-Type", "application/json;charset=utf8");

        HashMap<String, String> param = new HashMap();
        param.put("name", "wwj");
        param.put("password", "123");

        String result = doGet(httpClient, url, paramHeader, param, "UTF-8");
        System.out.println(result);
    }

}
